package com.lzy.managersystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.managersystem.mapper.CourseMapper;
import com.lzy.managersystem.pojo.Course;
import com.lzy.managersystem.service.CourseService;
import com.lzy.managersystem.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Resource
    private CourseMapper courseMapper;

    @GetMapping("/")
    public Result getCourse() {
        return Result.success(courseService.getAllUser());
    }

    @PostMapping("/inster")
    public Result insertUser(@RequestBody Course course) {
        System.out.println(course);
        int flag = courseService.insterUser(course);
        return Result.success(flag);
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        int flag = courseService.deleteUser(id);
        return Result.success(flag);
    }

    @PostMapping("/del/batch")
    public Result deleteBatchUser(@RequestBody List<Integer> ids) {
        int flag = courseService.deleteBatchUser(ids);
        return Result.success(flag);
    }

    @PutMapping("/")
    public Result UpdataUser(@RequestBody Course course) {
        int flag = courseService.updateUser(course);
        return Result.success(flag);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Course course) {
        return Result.success(courseMapper.updateById(course));
    }


    //查询所有用户（分页）
    @GetMapping("/page")
    public Result selectUserPage(@RequestParam Integer pageSize,
                                 @RequestParam Integer pageNum,
                                 @RequestParam(defaultValue = "") String name) {
//        QueryWrapper<Course> userQueryWrapper = new QueryWrapper<>();
//        if (!"".equals(name)) {
//            userQueryWrapper.like("name", name);
//        }
        Page<Course> page = courseService.pageInfo(new Page<>(pageNum, pageSize), name);
        return Result.success(page);

    }

    //根据老师id查询教授课程
    @GetMapping("/byid")
    public Result getCourseByid(@RequestParam("id") String id) {
        return Result.success(courseService.courseByteacherId(id));
    }

    //学生选课
    @PostMapping("/students/{courseId}/{studentId}")
    public Result updateStudentsCourse(@PathVariable("courseId") Integer courseId, @PathVariable("studentId") Integer studentId) {
        return Result.success(courseService.updateCourse(courseId, studentId));
    }

    //学生选课信息
    @GetMapping("/students/info")
    public Result selectStuCourse(@RequestParam("id") Integer id) {
        return Result.success(courseService.stuCourse(id));
    }


}
